package com.zzm.validate.validator;

import java.util.regex.Pattern;

import org.apache.commons.validator.Field;
import org.apache.commons.validator.util.ValidatorUtils;

public class RegexValidator {

	/**
	 * 使用该验证方法必须在 配置xml中声明正则式
	 * 	<var>
			<var-name>regex</var-name>
			<var-value>${your regex}</var-value>
		</var>
	 * @param bean 被验证的bean
	 * @param field @see org.apache.commons.validator.Field
	 * @return true is in range, otherwise false.
	 */
	public static boolean validateRegex(Object bean, Field field) {
		String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
		String regex = field.getVarValue("regex");
		return Pattern.matches(regex, value);
	}
}
